<?php

namespace app\modules\Order\models;

use Yii;
use yii\imagine\Image;
use Overtrue\Pinyin\Pinyin;
use yii\web\UploadedFile;

/**
 * This is the model class for table "order_customer".
 *
 * @property integer $id
 * @property integer $order_id
 * @property string $first_name
 * @property string $last_name
 * @property string $en_first_name
 * @property string $en_last_name
 * @property string $pinyin
 * @property integer $gender
 * @property string $birthday
 * @property string $country
 * @property string $passport
 * @property string $passport_add
 * @property string $passport_date
 * @property integer $passport_type
 * @property string $phone
 * @property string $remark
 * @property integer $status
 * @property integer $user_id
 * @property string $created_at
 * @property string $updated_at
 */
class OrderCustomer extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'order_customer';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['order_id'], 'required'],
            [['order_id', 'gender', 'passport_type', 'passport_check','status', 'user_id'], 'integer'],
            [['birthday', 'passport_grant', 'passport_date', 'created_at', 'updated_at'], 'safe'],
            [['first_name', 'last_name', 'en_first_name', 'en_last_name', 'nation'], 'string', 'max' => 32],
            [['pinyin', 'passport', 'passport_add', 'remark', 'passport_img', 'card_img'], 'string', 'max' => 255],
            [['country', 'phone', 'native', 'card_address'], 'string', 'max' => 64],
            [['card'], 'string', 'max' => 18],
        ];
    }

    // 保存前处理
    public function beforeSave($insert) {//{{{
        if($insert){
            $this->created_at = date("Y-m-d H:i:s");
            $this->user_id    = Yii::$app->user->id;
        }
		
		//  图片上传
        $upload = UploadedFile::getInstance($this, "passport_img");

        if($upload){
        	$dir = sprintf("uploads/passport/%s",date("Ym"));

        	if(!is_readable($dir)) {
        	    is_file($dir) or mkdir($dir,0755);
        	}

        	$suffix = $upload->extension;

        	$path = sprintf('%s/%s.%s', $dir, uniqid(true), $suffix);

        	$ret = $upload->saveAs($path);

        	if($ret){
        	    $this->passport_img = $path;

				//  图片压缩处理
				$imagine = new \Imagine\Imagick\Imagine();

				$imagine->open($path)
				   ->save($path, ['jpeg_quality' => 75]);
        	}
        }
         
		
		//  图片身份证 
        $upload2 = UploadedFile::getInstance($this, "card_img");

        if($upload2){
        	$dir = sprintf("uploads/card/%s",date("Ym"));

        	if(!is_readable($dir)) {
        	    is_file($dir) or mkdir($dir,0755);
        	}

        	$suffix = $upload2->extension;

        	$path = sprintf('%s/%s.%s', $dir, uniqid(true), $suffix);

        	$ret = $upload2->saveAs($path);

        	if($ret){
        	    $this->card_img = $path;

				//  图片压缩处理
				$imagine = new \Imagine\Imagick\Imagine();

				$imagine->open($path)
				   ->save($path, ['jpeg_quality' => 75]);
        	}
        }
         

        // 生成拼音 
		if(empty($this->en_first_name)){
        	$this->en_first_name = strtoupper(Pinyin::trans($this->first_name, ['accent'=>false]));
		}

		if(empty($this->en_last_name)){
        	$this->en_last_name = strtoupper(Pinyin::trans($this->last_name, ['accent'=>false]));
		}


        $name = sprintf("%s %s", $this->first_name, $this->last_name);
        $this->pinyin = Pinyin::trans($name,['accent'=>false]);

		// 判断客户资料信息是否完整
		if($this->first_name && $this->last_name && $this->en_first_name && $this->en_last_name && $this->pinyin && $this->gender
&& $this->birthday && $this->country && $this->passport && $this->passport_add && $this->passport_grant && $this->passport_date &&
$this->passport_type && $this->passport_img && $this->native ){
			if($this->complete != 1){
				$this->complete = 1;
				//$this->save();
			}
		}else{
			if($this->complete != 2){
				$this->complete = 2;
				//$this->save();		
			}
		}

        return parent::beforeSave($insert);
    }//}}}

    // 保存后执行
	public function afterSave($insert, $changedAttributes){//{{{

		parent::afterSave($insert, $changedAttributes);

		if($insert) {
			$desc = sprintf("添加新客户  %s%s" , $this->first_name, $this->last_name);
			Yii::$app->logs->info($this->order_id, 4, $desc);
		}else{
			// 修改
			if($this->status == 2){// 取消
				
				$desc = sprintf("取消客户  %s%s" , $this->first_name, $this->last_name);
				Yii::$app->logs->info($this->order_id, 4, '', $changedAttributes);
			}else{				  // 普通修改
				
				$desc = sprintf("修改客户  %s%s" , $this->first_name, $this->last_name);
				Yii::$app->logs->info($this->order_id, 4, $desc, $changedAttributes);
			}
		}


	}//}}}



    //获取人员对应的房间
    public function getRoomCustomer(){
         return $this->hasOne(OrderRoomCustomer::className(), ['customer_id' => 'id']);
    }

	// 获取人员对应的订单
	public function getOrder(){
		
         return $this->hasOne(Order::className(), ['id' => 'order_id']);
	}

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app.order', 'ID'),
            'order_id' => Yii::t('app.order', 'Order ID'),
            'first_name' => Yii::t('app.order', 'First Name'),
            'last_name' => Yii::t('app.order', 'Last Name'),
            'en_first_name' => Yii::t('app.order', 'En First Name'),
            'en_last_name' => Yii::t('app.order', 'En Last Name'),
            'pinyin' => Yii::t('app.order', 'Pinyin'),
            'gender' => Yii::t('app.order', 'Gender'),
            'birthday' => Yii::t('app.order', 'Birthday'),
            'country' => Yii::t('app.order', 'Country'),
            'passport' => Yii::t('app.order', 'Passport'),
            'passport_add' => Yii::t('app.order', 'Passport Add'),
            'passport_grant' => Yii::t('app.order', 'Passport Grant'),
            'passport_date' => Yii::t('app.order', 'Passport Date'),
            'passport_type' => Yii::t('app.order', 'Passport Type'),
            'passport_check' => Yii::t('app.order', 'Passport Check'),
            'phone' => Yii::t('app.order', 'Phone'),
            'remark' => Yii::t('app.order', 'Remark'),
            'status' => Yii::t('app.order', 'Status'),
            'user_id' => Yii::t('app.order', 'User ID'),
            'created_at' => Yii::t('app.order', 'Created At'),
            'updated_at' => Yii::t('app.order', 'Updated At'),
            'passport_img' => Yii::t('app.order', 'Passport Img'),
            'native' => Yii::t('app.order', 'Native'),
            'card' => Yii::t('app.order', '身份证号'),
            'card_address' => Yii::t('app.order', '身份证签发地'),
            'card_img' => Yii::t('app.order', '身份证或户口照片'),
            'nation' => Yii::t('app.order', '民族'),
        ];
    }
}
